<?php
class Download{
	public static function generate_favorite_header($addressarray){
		header('Content-Disposition: attachment; filename="search-wget.sh";');
		header("Content-Type: application/octet-stream" );
		print "#!/bin/bash";
		print "\n";
		if(count($addressarray)>=1){
			foreach ($addressarray as $id){
				$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".name,".DATABASE_TABLE_FAV.".fullpath,".DATABASE_TABLE_FILE.".server FROM ".DATABASE_TABLE_FAV." JOIN ".DATABASE_TABLE_FILE." ON ".DATABASE_TABLE_FAV.".fullpath = ".DATABASE_TABLE_FILE.".fullpath WHERE ".DATABASE_TABLE_FAV.".id = $id  ") or die(mysql_error());
				while($query= mysql_fetch_array($sql_query)){
					$array_id = Download::get_id($query['server'],$user->login,md5($query['fullpath']));
					print 'wget '.$array_id[1].'?id='.$array_id[0].'  -O "'.$query['name'].'"';
					print "\n";
				}
			}
		}else{
			print "No file selected";
		}
	}
	
	public static function get_id($server,$user,$md5){
		$sql_query=mysql_query("SELECT address,pin FROM ".DATABASE_TABLE_SERVER." WHERE name like '".$server."' ORDER BY `name` ASC") or die(mysql_error());
			while ($query = mysql_fetch_array($sql_query) ){
				$address=$query['address'];
				$pin = $query['pin'];
			}
			$f=@fopen($address."?action=getid&user=$user&film=".$md5."&hash=".md5($pin),'r');
			//print $address."?action=getid&user=$user&film=".$md5."&hash=".md5($pin);exit;
				if ($f != False){
						$e="";
						$i=0;
						while (strpos($e,"id") == "" and $i<100){
							$e=fgets($f);
							$i++;
						}
						$e=trim($e);
						$id = substr($e,(strpos($e,"<id>")+4),(strpos($e,"</id>")-4));
						$array_id[0]=$id;
						$array_id[1]=$address;
						//print $id; exit();
						return $array_id;
					}
			return false;
	}
	
	public static function send_id($array_id){
		header('Location: '.$array_id[1].'?id='.$array_id[0]);
	}
	
}
?>